package com.gzhryc.shared_device.oem.h5.mng.controllers.ops;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.jdbc.DateBranchHelper;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.payment.dao.db.AlipayOrder;
import com.gzhryc.shared_device.common.payment.services.AlipayOrderService;
import com.gzhryc.shared_device.common.payment.services.dto.AlipayOrderSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

public class AlipayOrderController {

    @Route(value = { "/ops/order/ali/pay/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("dateBranchItemList", DateBranchHelper.self().getYearDateBranchItems());
        return "/ops/order/ali_pay/list.html";
    }

    @Route(value = "/ops/order/ali/pay/list.data")
    public ListResult<AlipayOrder> listData(HttpServletRequest request) {
        ListResult<AlipayOrder> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        AlipayOrderSearch search = WebUtils.getParameter(request, AlipayOrderSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        Date dateBranch = new Date();
        if(StringTools.isNotBlank(search.getDateBranchId())){
            dateBranch = DateBranchHelper.getDateByItemId(search.getDateBranchId());
        }

        result.getBody().setTotal(AlipayOrderService.self(dateBranch).countBySearch(search));
        List<AlipayOrder> data = AlipayOrderService.self(dateBranch).findBySearch(search,pageNo,pageLimit);
        if(data != null && data.size() > 0){
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(data);
        }
        return result;
    }

    @Route(value = { "/ops/order/ali/pay/info.html" })
    public String info(HttpServletRequest request) {
        String tradeNo = WebUtils.getString(request, "tradeNo");
        Date createDate = WebUtils.getDate(request,"createDate");
        if(createDate == null){
            createDate = new Date();
        }
        AlipayOrder payOrder = AlipayOrderService.self(createDate).getByTradeNo(tradeNo);
        WebUtils.setAttribute(request,payOrder);
        return "/ops/order/ali_pay/info.html";
    }
    
    @Route(value = { "/ops/order/ali/pay/close.do" })
    public AjaxResult payClose(HttpServletRequest request) {
    	AjaxResult result = new AjaxResult();
        String tradeNo = WebUtils.getString(request, "tradeNo");
        Date createDate = WebUtils.getDate(request,"createDate");
        if(createDate == null){
            createDate = new Date();
        }
        AlipayOrder payOrder = AlipayOrderService.self(createDate).getByTradeNo(tradeNo);
        if(payOrder != null) {
        	WebUtils.setAttribute(request,payOrder);
        }
        return result;
    }

}
